
package vistas;

import BasesdDatos.conectar;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import humanos.Usuario;

/**
 *
 * @author andres
 */
public class acceso extends javax.swing.JDialog {


    public acceso() {
        initComponents();
        setTitle("ACCESO");
        setVisible(true);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        capturausuario = new javax.swing.JTextField();
        aceptar = new javax.swing.JButton();
        borrar = new javax.swing.JButton();
        jXBusyLabel3 = new org.jdesktop.swingx.JXBusyLabel();
        jXBusyLabel4 = new org.jdesktop.swingx.JXBusyLabel();
        capturaclave = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        aceptar.setText("Aceptar");
        aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarActionPerformed(evt);
            }
        });

        borrar.setText("Borrar");
        borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarActionPerformed(evt);
            }
        });

        jXBusyLabel3.setText("Usuario:");

        jXBusyLabel4.setText("Clave:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXBusyLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXBusyLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(capturausuario)
                    .addComponent(capturaclave, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(borrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(aceptar, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
                .addGap(45, 45, 45))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(capturausuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(aceptar)
                    .addComponent(jXBusyLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(borrar)
                            .addComponent(jXBusyLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(capturaclave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 41, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void aceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarActionPerformed

        conectar conexion= new conectar();
	Connection conn=null;
      
      Usuario IngresaUsuario = new Usuario();
      if(evt.getSource() == aceptar); 
      aceptar.setEnabled(false);
      IngresaUsuario.setlogin(capturausuario.getText());
      IngresaUsuario.setclave(capturaclave.getText());
       
      try{
           
        conn = conexion.getConexion();
        String sql = " SELECT * FROM verificaU where clave= '"+IngresaUsuario.getClave()+"'";
        PreparedStatement  ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        
        if(rs.next()){
        String claveLocal = rs.getString("clave");
        
                     
             if(claveLocal == null ? capturaclave.getText() == null : claveLocal.equals(capturaclave.getText())){
                 
                 GestionVehiculo gvehiculo = new GestionVehiculo();
                 gvehiculo.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                 gvehiculo.setLocationRelativeTo(null);
                 setVisible(false);
                 
                 
             }
                               
             else if(!claveLocal.equals(capturaclave.getText())){
            
                JOptionPane.showMessageDialog(null, "Verifique Usuario o Contraseña!!");
        }
       
        conn.close();
        Toolkit.getDefaultToolkit().beep();
        aceptar.setEnabled(true);
             
     }     
       
        
                      
      }
       
      
      catch(SQLException | HeadlessException e){
          JOptionPane.showMessageDialog(null, e);
      }
         
    }//GEN-LAST:event_aceptarActionPerformed

    private void borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarActionPerformed

        capturaclave.setText(null);
        capturausuario.setText(null);
        // TODO add your handling code here:
    }//GEN-LAST:event_borrarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.JButton borrar;
    private javax.swing.JPasswordField capturaclave;
    private javax.swing.JTextField capturausuario;
    private org.jdesktop.swingx.JXBusyLabel jXBusyLabel3;
    private org.jdesktop.swingx.JXBusyLabel jXBusyLabel4;
    // End of variables declaration//GEN-END:variables
}
